class MUtil{
    getServerUrl(){
        //return "http://www.choisaaaa.campuso2o.com";
        return "http://www.choisaaaa.shop";
    }
    request(param){
        let serverPath = this.getServerUrl();
        return new Promise((resolve, reject) =>{
            $.ajax({
                type        : param.type        || 'get',
                url         :serverPath + param.url         || '',
                dataType    : param.dataType    || 'json',
                data        : param.data        || null,
                xhrFields:{
                    withCredentials: true
                },
                crossDomain: true,
                success : res => {
                   if(0 === res.status){//数据请求成功
                       typeof resolve === "function" && resolve(res.data,res.msg);
                   }
                   else if (10 === res.status){//需要登录
                       this.doLogin();
                   }
                   else if(1 === res.status){
                       typeof reject === "function" && reject(res.msg);
                   }
                   else{
                       typeof reject === "function" && reject(res.data,res.msg);
                   }
                },
                error :err =>{
                    typeof reject === "function" && reject(err.data,err.msg);
                }

            });
        });
    }
    doLogin(){
        window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
    }
    // 获取URL参数
    getUrlParam(name){
        // param=123&param1=456
        let queryString = window.location.search.split('?')[1] || '',
            reg         = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
            result      = queryString.match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    }
    // 成功提示
    successTips(successMsg){
        alert(successMsg || '操作成功！');
    }
    // 错误提示
    errorTips(errMsg){
        alert(errMsg || '好像哪里不对了~');
    }
    // 本地存储
    setStorage(name, data){
        let dataType = typeof data;
        // json对象
        if(dataType === 'object'){
            window.localStorage.setItem(name, JSON.stringify(data));
        }
        // 基础类型
        else if(['number','string','boolean'].indexOf(dataType) >= 0){
            window.localStorage.setItem(name, data);
        }
        // 其他不支持的类型
        else{
            alert('该类型不能用于本地存储');
        }
    }
    // 取出本地存储内容
    getStorage(name){
        let data = window.localStorage.getItem(name);
        if(data){
            return JSON.parse(data);
        }
        else{
            return '';
        }
    }
    // 删除本地存储
    removeStorage(name){
        window.localStorage.removeItem(name);
    }
}
export default MUtil;